from faker import Faker
from typing import Any, Dict, List, Tuple
from app.core.config import settings

class DataGenerator:
    def __init__(self):
        self.faker = Faker(['zh_CN'])
        self.type_keywords = {
            'person': ['人', '个人', '用户', '客户', '员工'],
            'company': ['公司', '企业', '单位', '机构', '组织'],
            'article': ['文章', '博客', '新闻', '帖子', '内容'],
            'product': ['产品', '商品', '货物', '物品'],
            'order': ['订单', '交易', '购买', '下单'],
            'review': ['评论', '评价', '反馈', '点评'],
            'bank_account': ['银行', '账户', '账号', '存款'],
            'education': ['教育', '学生', '学校', '学历', '成绩'],
            'social_media': ['社交', '媒体', '账号', '粉丝']
        }
        
    def generate_person(self) -> Dict[str, Any]:
        return {
            "name": self.faker.name(),
            "address": self.faker.address(),
            "phone": self.faker.phone_number(),
            "email": self.faker.email(),
            "birth_date": self.faker.date_of_birth().isoformat(),
            "job": self.faker.job()
        }
        
    def generate_company(self) -> Dict[str, Any]:
        return {
            "company_name": self.faker.company(),
            "company_address": self.faker.address(),
            "business_type": self.faker.company_prefix(),
            "contact": self.faker.phone_number(),
            "website": self.faker.domain_name(),
            "established_date": self.faker.date_this_century().isoformat()
        }
        
    def generate_article(self) -> Dict[str, Any]:
        return {
            "title": self.faker.sentence(),
            "content": self.faker.text(max_nb_chars=200),
            "author": self.faker.name(),
            "date": self.faker.date_this_year().isoformat(),
            "category": self.faker.word(),
            "tags": self.faker.words(3)
        }
        
    def generate_product(self) -> Dict[str, Any]:
        return {
            "product_name": self.faker.word() + " " + self.faker.word(),
            "description": self.faker.text(max_nb_chars=100),
            "price": round(self.faker.random_number(4) / 100, 2),
            "category": self.faker.word(),
            "sku": self.faker.ean13(),
            "stock": self.faker.random_int(0, 1000)
        }
        
    def generate_order(self) -> Dict[str, Any]:
        return {
            "order_id": self.faker.uuid4(),
            "customer_name": self.faker.name(),
            "order_date": self.faker.date_time_this_month().isoformat(),
            "total_amount": round(self.faker.random_number(4) / 100, 2),
            "status": self.faker.random_element(['pending', 'processing', 'shipped', 'delivered']),
            "shipping_address": self.faker.address()
        }
        
    def generate_review(self) -> Dict[str, Any]:
        return {
            "user_name": self.faker.name(),
            "rating": self.faker.random_int(1, 5),
            "comment": self.faker.text(max_nb_chars=150),
            "date": self.faker.date_time_this_year().isoformat(),
            "product_id": self.faker.uuid4()
        }
        
    def generate_bank_account(self) -> Dict[str, Any]:
        return {
            "account_holder": self.faker.name(),
            "account_number": self.faker.bban(),
            "bank_name": self.faker.company(),
            "branch": self.faker.city(),
            "balance": round(self.faker.random_number(6) / 100, 2),
            "account_type": self.faker.random_element(['savings', 'checking', 'investment'])
        }
        
    def generate_education(self) -> Dict[str, Any]:
        return {
            "student_name": self.faker.name(),
            "school": self.faker.company(),
            "major": self.faker.job(),
            "start_date": self.faker.date_this_decade().isoformat(),
            "end_date": self.faker.date_this_decade().isoformat(),
            "gpa": round(self.faker.random.uniform(2.0, 4.0), 2)
        }
        
    def generate_social_media(self) -> Dict[str, Any]:
        return {
            "username": self.faker.user_name(),
            "profile_name": self.faker.name(),
            "bio": self.faker.text(max_nb_chars=100),
            "followers": self.faker.random_int(0, 100000),
            "following": self.faker.random_int(0, 1000),
            "posts": self.faker.random_int(0, 500)
        }

    def generate_data(self, data_type: str, count: int = 1) -> List[Dict[str, Any]]:
        generator_map = {
            'person': self.generate_person,
            'company': self.generate_company,
            'article': self.generate_article,
            'product': self.generate_product,
            'order': self.generate_order,
            'review': self.generate_review,
            'bank_account': self.generate_bank_account,
            'education': self.generate_education,
            'social_media': self.generate_social_media
        }
        
        if data_type not in generator_map:
            raise ValueError(f"不支持的数据类型: {data_type}")
            
        return [generator_map[data_type]() for _ in range(count)]
